﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Storm.TextEditor.Editor
{
    /// <summary>
    /// Represents the UITypeEditor for the TextStyle class.
    /// </summary>
    public class TextStyleUIEditor
        : UITypeEditor
    {
        #region Fields

        private IWindowsFormsEditorService editorService = null;

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Edits the given value object.
        /// </summary>
        /// <param name="context">ITypeDescriptorContext.</param>
        /// <param name="provider">IServiceProvider.</param>
        /// <param name="value">Value to edit.</param>
        /// <returns>Edited value.</returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context != null && context.Instance != null && provider != null)
            {
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                if (editorService != null)
                {
                    TextStyle style = value as TextStyle;
                    using (TextStyleDesignerDialog textStyleDesigner = new TextStyleDesignerDialog(style))
                    {
                        context.OnComponentChanging();
                        if (editorService.ShowDialog(textStyleDesigner) == DialogResult.OK)
                        {
                            context.OnComponentChanged();
                            return style;
                        }
                    }
                }
            }

            return value;
        }

        /// <summary>
        /// Paints the value in the given PaintValueEventArgs.
        /// </summary>
        /// <param name="e">PaintValueEventArgs.</param>
        public override void PaintValue(PaintValueEventArgs e)
        {
            TextStyle textStyle = e.Value as TextStyle;
            using (SolidBrush brush = new SolidBrush(textStyle.BackColor))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }

            FontStyle fontStyle = FontStyle.Regular;

            if (textStyle.Bold == true)
                fontStyle |= FontStyle.Bold;

            if (textStyle.Italic == true)
                fontStyle |= FontStyle.Italic;

            if (textStyle.Underline == true)
                fontStyle |= FontStyle.Underline;

            Font f = new Font("Arial", 8f, fontStyle);
            using (SolidBrush brush = new SolidBrush(textStyle.ForeColor))
            {
                e.Graphics.DrawString("abc", f, brush, e.Bounds);
            }

            f.Dispose();
        }

        /// <summary>
        /// Override - simply returns true.
        /// </summary>
        /// <param name="context"></param>
        /// <returns>true</returns>
        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Returns the editing style for the given context.
        /// </summary>
        /// <param name="context">Context to return editing style for.</param>
        /// <returns>The editing style for the given context.</returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        #endregion

        #endregion

        /// <summary>
        /// Initializes a new instance of TextStyleUIEditor.
        /// </summary>
        public TextStyleUIEditor()
        {
        }
    }
}
